/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.goods.service.impl;

import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.goods.domain.TGoods;
import com.xunyin.modules.goods.service.TGoodsService;
import com.xunyin.modules.goods.service.dto.TGoodsDto;
import com.xunyin.modules.goods.service.dto.TGoodsQueryCriteria;
import com.xunyin.modules.goods.service.mapper.TGoodsMapper;
import com.xunyin.modules.goods.vo.GoodsInfoVo;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author admin
 * @date 2022-10-28
 */
@Slf4j
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "tGoods")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TGoodsServiceImpl extends BaseServiceImpl<TGoodsMapper, TGoods> implements TGoodsService {

    private final IGenerator generator;

    @Autowired
    TGoodsMapper tGoodsMapper;


    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TGoodsQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TGoods> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<TGoods> queryAll(TGoodsQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TGoods.class, criteria));
    }

    /**
     * 根据 linkId 获取茶室柜台商品信息
     * @param goodsLinkId
     * @return
     */
    public GoodsInfoVo getGoodsByLinkId(Integer goodsLinkId) {
        GoodsInfoVo goodsInfo = tGoodsMapper.getGoodsInfoByLinkId(goodsLinkId);
        return goodsInfo;
    }


    @Override
    public void download(List<TGoodsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TGoodsDto tGoods : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("产品名称", tGoods.getGoodsName());
            map.put("产品详情", tGoods.getGoodsDetail());
            map.put(" containerNum", tGoods.getContainerNum());
            map.put("金额", tGoods.getPrice());
            map.put("库存", tGoods.getStock());
            map.put("上下架 0 下架 1 上架", tGoods.getPublishStatus());
            map.put("是否折扣 0 不能 1 能", tGoods.getIsDiscount());
            map.put(" images", tGoods.getImages());
            map.put("创建时间", tGoods.getCreatedTime());
            map.put("创建人", tGoods.getCreatedUserId());
            map.put("修改时间", tGoods.getUpdatedTime());
            map.put("修改人", tGoods.getUpdatedUserId());
            map.put("状态", tGoods.getStatus());
            map.put("品牌名称", tGoods.getBrand());
            map.put("类别ID", tGoods.getCategoryId());
            map.put("类别名称", tGoods.getCategoryName());
            map.put("规格", tGoods.getSizeDesc());
            map.put("创建日期", tGoods.getCreateTime());
            map.put("更新日期", tGoods.getUpdateTime());
            map.put("删除日期", tGoods.getDeleteTime());
            map.put("创建用户ID", tGoods.getCreateUserId());
            map.put("更新用户ID", tGoods.getUpdateUserId());
            map.put(" isDel", tGoods.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
